package com.nut.article.common;

import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

@Data
@NoArgsConstructor
public class Result<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    private static final Result SUCCESS = new Result(0, null);
    private static final Result ERROR = new Result(-1, null);

    //状态标识 0成功  其他值失败
    private Integer code = 0;
    private String message = "操作成功！";
    private T result;

    private Result(T t) {
        this.code = 0;
        this.result = t;
    }

    private Result(int code, String message) {
        this.code = code;
        this.message = message;
    }

    public static <T> Result<T> ok() {
        return SUCCESS;
    }

    public static <T> Result<T> ok(T t) {
        return new Result(t);
    }

    public static <T> Result<T> fail() {
        return ERROR;
    }

    public static <T> Result<T> fail(String message) {
        return new Result<>(-1, message);
    }

    public boolean isSuccess() {
        return this.code == 0 ? true : false;
    }

}
